package com.xjj.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjj.entity.*;
import com.xjj.service.mapper.LeaveMapper;
import com.xjj.service.mapper.StudentMapper;
import com.xjj.service.service.LeaveService;
import com.xjj.util.exception.BusinessException;
import com.xjj.util.utils.JwtUtil;
import com.xjj.vo.LeaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author : 王泰元
 * @date : 2022/10/24 17:17
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {
    @Autowired
    private LeaveMapper leaveMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean<Leave> getByPage(LeaveVO leaveVo) {

        List<Integer> ids = new ArrayList<>();
        if (StrUtil.isNotBlank(leaveVo.getLeave().getStudentName())) {
            for (Student student : studentMapper.selectList(new QueryWrapper<Student>().like("name", leaveVo.getLeave().getStudentName()))) {
                ids.add(student.getId());
            }
        }
        leaveVo.setSids(ids);
        PageBean<Leave> pageBean = new PageBean<>();

        Page p = PageHelper.startPage(leaveVo.getPageNum(), leaveVo.getPageSize());
        List<Leave> leaveList = leaveMapper.getByCondition(leaveVo);
        log.debug("leaveList:{}" + leaveList);
        pageBean.setData(leaveList);
        pageBean.setCurrPage(leaveVo.getPageNum());
        pageBean.setCurrPageSize(leaveList.size());
        pageBean.setPageSize(leaveVo.getPageSize());
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    @Override
    public void authLeave(Leave leave, String token) throws JsonProcessingException {
        Manager manager = JwtUtil.getManager(token);
        QueryWrapper<Leave> klassIdWrapper = new QueryWrapper<>();
        klassIdWrapper.select("klass_id").eq("id", leave.getId());
        Integer klassId = getOne(klassIdWrapper).getKlassId();

        if (leaveMapper.isYourTeacher(manager.getId(), klassId) == 0) {
            throw new BusinessException("您不是本班教师，无权操作");
        }

        leave.setAuthTime(util.GetCurrentTime.getCurrentTime());
        updateById(leave);
    }

    @Override
    public List<Leave> getByStudent(Integer studentId, Integer status) {
        LeaveVO leaveVO = new LeaveVO();
        leaveVO.setSids(Collections.singletonList(studentId));
        Leave leave = new Leave();
        leave.setStatus(status == null ? null : String.valueOf(status));
        leaveVO.setLeave(leave);
        return leaveMapper.getByCondition(leaveVO);
    }

    @Override
    public Integer addLeave(Leave leave) {
        leave.setStatus("0");
        leave.setSendTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        leave.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(Long.parseLong(leave.getStartTime()))));
        leave.setFinishTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(Long.parseLong(leave.getFinishTime()))));
        leave.setKlassId(studentMapper.selectById(leave.getStudentId()).getKlassId());
        save(leave);
        return leave.getId();
    }

    @Override
    public boolean checkLeave(Integer id, Integer parentId) {
        // 获取当前时间
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        boolean success = update()
                .eq("id", id)
                .setSql("status = 2")
                .setSql("check_time = '" + now + "'")
                .update();
        if (!success) {
            throw new BusinessException("服务器异常");
        }

        return true;
    }


}
